/**
 * 我的收文js
 * Created by mhy on 2017/1/9.
 */
$(document).ready(function () {

    var serious_doc = 0;    // 是否有加急的未读公文

    //get received document count and initial tables
    $.ajax({
        type: 'GET',
        url: '../document/count/inbox',
        contentType: "application/json",
        dataType: 'JSON',
        success: function (json) {
            var inboxCount = json.data.received;
            var pagesNumber = parseInt(inboxCount / 10 + 1);
            var $checkbox_paginate = $("#datatable-checkbox_paginate");
            var $ulObj = $(".pagination",$checkbox_paginate);

            createPaging($checkbox_paginate,pagesNumber);   //初始化分页元素
            displayInboxList(1);    //初始化表格

            var $liObj = $("li",$ulObj);

            for(var i = 1;i < $liObj.length - 1;i++){
                $liObj.eq(i).children("a").attr("onclick",'displayInboxList('+ i +')');
            }   //end for
        },
    });


});

function initTable(data) {
    var $datatable = $('#datatable-checkbox');
    $datatable.dataTable({
        data:data,
        "createdRow": function ( row, data, index ) {
            $('td', row).css("text-align","center").css("vertical-align","middle");
        },
        columns:[
            {"sTitle": "发送时间"},
            {"sTitle": "公文标题"},
            {"sTitle": "公文号"},
            {"sTitle": "公文文种"},     //3
            {"sTitle": "发送人"},
            {"sTitle": "紧急程度"},
            {"sTitle": "公文状态"}
        ],
        "ordering": false,
        deferRender: true,
        paging:false
    } );
}

// 公文详情阅读
function readDocument(doc_id) {
    $.ajax({
        type: 'POST',
        url: '../document/receive/detailed/' + doc_id,
        contentType: "application/json",
        dataType: 'JSON',
        success: function (json,status) {
            window.location.href = "../document/my_received_content.html?doc_id=" + doc_id;
            if(json.err_msg != null){
                alert(json.err_msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

        },
    });
}


// 公文删除
function deleteDocument(doc_id) {
    if(confirm("您是否要删除这条记录?")){
        $.ajax({
            type: 'DELETE',
            url: '../document/receive/delete/' + doc_id,
            contentType: "application/json",
            dataType: 'JSON',
            success: function (json,status) {
                if(json.err_msg != null){
                    alert(json.err_msg);
                }else{
                    window.location.href = "../document/my_received_doc.html";
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            },
        });
    }
}

/* 显示收文列表
 * @param:pagesNumber
 * */
function displayInboxList(pagesNumber) {
    clearTables($('#datatable-checkbox'));
    adjustPagination($('#pagination'),pagesNumber);

    // 获取收文列表
    $.ajax({
        type: 'POST',
        url: '../document/list/inbox',
        contentType: "application/json",
        dataType: 'JSON',
        data:JSON.stringify({
            "pageNumber": parseInt(pagesNumber),
        }),
        success: function (json,status) {
            var datas = new Array();
            var arr = new Array();
            var docs = json.data.documents;
            for(var i = 0;i<docs.length;i++){
                arr = [];
                arr[0] = docs[i].sent_time;
                arr[1] = docs[i].title;
                arr[2] = docs[i].number;
                arr[3] = docs[i].type;
                arr[4] = docs[i].writer;
                arr[5] = "<p style='color: indianred;'>紧急公文</p>";
                if(docs[i].serious_type == "SERIOUS"){
                    arr[5] = "<p style='color: indianred;'>紧急公文</p>";
                }else if(docs[i].serious_type == "NOT_SERIOUS"){
                    arr[5] = "非紧急公文";
                }
                if(docs[i].revoked == true){
                    arr[6] = "<button type='button' class='btn btn-success'  disabled='disabled'>该公文已被撤销</button><br/>";
                }else{
                    arr[6] = "<button type='button' class='btn btn-success' onclick='readDocument("+ docs[i].id +")'>公文阅读</button>";
                }
                // console.log("already_read: " + docs[i].already_read);
                if(docs[i].already_read == true){
                    arr[6] += "<button type='button' class='btn btn-danger' onclick='deleteDocument("+ docs[i].id +")'>删除记录</button><br/>";
                    arr[6] += "已读公文";
                }else{
                    arr[6] += "<p style='color: darkred'>未读公文</p>";
                }
                if(docs[i].serious_type == "SERIOUS" && docs[i].already_read == false && docs[i].revoked == false){
                    serious_doc = 1;
                }
                datas[i] = arr;
            }
            initTable(datas);
            if(json.err_msg != null){
                alert(json.err_msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(errorThrown);
        },
        cache: false,
    });
}